package com.syl.qianyin.spider.service;

import com.syl.framework.frametools.utils.RedisObjectUtils;
import com.syl.qianyin.spider.beans.IpProxyBean;
import com.syl.qianyin.spider.constants.RedisKeyConstant;
import com.syl.qianyin.spider.exend.CustomHttpClientDownloader;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.downloader.HttpClientRequestContext;
import us.codecraft.webmagic.proxy.Proxy;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 验证获取到的ip池
 *
 * @author syl
 * @create 2019-05-12 17:30
 **/
@Service
public class VerifyIpPoolService {
    private static final Logger LOG = LoggerFactory.getLogger(VerifyIpPoolService.class);

    @Autowired
    private RedisObjectUtils redisObjectUtils;

    public void start(){
        List<IpProxyBean> listAllCache = redisObjectUtils.getListAllCache(RedisKeyConstant.MAP_IP_POO_UNCONFIRMED, IpProxyBean.class);
        String url = "http://www.baidu.com";
        Site site = new Site()
            .setTimeOut(3*1000)
            .setDomain(url)
        ;
        CustomHttpClientDownloader customHttpClientDownloader = new CustomHttpClientDownloader();
        CloseableHttpClient httpClient = customHttpClientDownloader.getHttpClient(site);
        Request request = new Request();
        request.setUrl(url);
        int errorSize = 0;
        for (IpProxyBean ipProxyBean : listAllCache) {
            Proxy proxy = new Proxy(ipProxyBean.getHost(), ipProxyBean.getPort());
            HttpClientRequestContext requestContext = customHttpClientDownloader.getHttpUriRequestConverter().convert(request, site, proxy);
            try {
                CloseableHttpResponse response = httpClient.execute(requestContext.getHttpUriRequest(), requestContext.getHttpClientContext());
                LOG.info("response {}", response);
                redisObjectUtils.pushListCache(RedisKeyConstant.MAP_IP_POO_VALID, ipProxyBean.setVerify(true).setVerifyTime(new Date()));
            } catch (IOException e) {
//                e.printStackTrace();
                LOG.error("代理ip已失效 无法连接 代理对象 {}", ipProxyBean);
                errorSize++;
            }
        }
        LOG.info("完成代理ip池检验 共检验 {} 个代理 失败了 {} 个 当前可用个数 {}", listAllCache.size(), errorSize, (listAllCache.size() - errorSize));
    }

}
